package com.qf.controller;


import com.github.pagehelper.PageHelper;
import com.qf.dao.CarsDao;
import com.qf.pojo.Msg;
import com.qf.pojo.Orders;
import com.qf.pojo.User;
import com.qf.service.OrderService;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    private UserService userService;
    @Autowired(required = false)
    private OrderService orderService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Msg<User> all(int page, int limit) {
        PageHelper.startPage(page,limit);

        List<User> ulist = userService.selectAll();
        Msg<User> m =new Msg<>();
        m.setData(ulist);
        m.setCount(20);
        return m;
    }

    @RequestMapping("/denglu.do")
    @ResponseBody//个人信息页面展示
    public Msg myInfo(HttpServletRequest request){

        HttpSession session = request.getSession();
//       User user1= new User();
//       user1.setTel("123456");
//       user1.setEmail("66@6.com");
//        session.setAttribute("user",user1);
        Object ouser = session.getAttribute("user");
        Msg<Object> m = new Msg<>();
        if (ouser!=null &&ouser instanceof User){
            User user =(User)ouser;
            m.setCode(1);
            m.setData(user);
        }
        return m;
    }
    @RequestMapping("/upEmail")
    @ResponseBody//邮箱修改
    public Msg upEmail(String email,HttpSession session){
        Object ouser  =session.getAttribute("user");
        Msg<Object> m = new Msg<>();
        if (ouser!=null &&ouser instanceof User){
            User user =(User)ouser;
            int res = userService.upEmail(email,user.getId());
            m.setCode(res);
        }
        return m;
    }


    @RequestMapping("/update.do")
    @ResponseBody//个人信息用户页面修改
    public Msg upInfo(String tel,HttpServletRequest request){
        HttpSession session = request.getSession();
        Object ouser  =session.getAttribute("user");
        Msg<Object> m = new Msg<>();
        if (ouser!=null &&ouser instanceof User){
            User user =(User)ouser;
            int res = userService.updateTel(tel,user.getId());
            m.setCode(res);
        }
        return m;
    }
    //登录
    @RequestMapping("/login.do")
    @ResponseBody//个人信息页面修改
    public Msg login(String tel, String password ,HttpSession session){
        Msg<Object> m = new Msg<>();
        User user = userService.selectOne(tel,password);
        if (user !=null){
            session.setAttribute("user",user);
            m.setCode(1);
        }
        return m;
    }


}
